package com.mit.facade;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.mit.common.Constants;
import com.mit.common.RequestContext;
import com.mit.common.ResultContext;
import com.mit.common.Sequence;
import com.mit.common.design.dto.FabricDTO;
import com.mit.common.design.dto.StyleColorDTO;
import com.mit.common.design.dto.StyleDTO;
import com.mit.common.design.dto.TemplateDTO;
import com.mit.common.design.dto.TemplatePartDTO;
import com.mit.common.exception.BaseException;
import com.mit.common.exception.NoRecordUpdateException;
import com.mit.common.maintenance.dto.ProductColorDTO;
import com.mit.common.maintenance.dto.SeasonDTO;
import com.mit.common.purchase.dto.InquiryItemDTO;
import com.mit.common.purchase.dto.InquiryTaskDTO;
import com.mit.common.security.dto.UserDTO;
import com.mit.common.token.DesignTokens;
import com.mit.common.token.Tokens;
import com.mit.service.core.dao.ISequenceDAO;
import com.mit.service.design.dao.IFabricDAO;
import com.mit.service.design.dao.IStyleColorDAO;
import com.mit.service.design.dao.IStyleMasterDAO;
import com.mit.service.design.dao.ITemplateDAO;
import com.mit.service.design.dao.ITemplatePartDAO;
import com.mit.service.maintenance.dao.ISeasonDAO;
import com.mit.service.purchase.dao.IInquiryItemDAO;
import com.mit.service.purchase.dao.IInquiryTaskDAO;

public class DesignFacade implements IDesignFacade {

	private Logger logger = Logger.getLogger(DesignFacade.class);
	
	private ISequenceDAO sequenceDAO;
	private IStyleMasterDAO styleMasterDAO;
	private IStyleColorDAO styleColorDAO;
	private IFabricDAO fabricDAO;
	private ISeasonDAO seasonDAO;
	private ITemplateDAO templateDAO;
	private ITemplatePartDAO templatePartDAO;
	private IInquiryTaskDAO inquiryTaskDAO;
	private IInquiryItemDAO inquiryItemDAO;
	
	
	public void setInquiryTaskDAO(IInquiryTaskDAO inquiryTaskDAO) {
		this.inquiryTaskDAO = inquiryTaskDAO;
	}

	public void setInquiryItemDAO(IInquiryItemDAO inquiryItemDAO) {
		this.inquiryItemDAO = inquiryItemDAO;
	}

	public void setTemplateDAO(ITemplateDAO templateDAO) {
		this.templateDAO = templateDAO;
	}

	public void setTemplatePartDAO(ITemplatePartDAO templatePartDAO) {
		this.templatePartDAO = templatePartDAO;
	}
	
	public void setSeasonDAO(ISeasonDAO seasonDAO) {
		this.seasonDAO = seasonDAO;
	}	

	public void setSequenceDAO(ISequenceDAO sequenceDAO) {
		this.sequenceDAO = sequenceDAO;
	}

	public void setStyleMasterDAO(IStyleMasterDAO styleMasterDAO) {
		this.styleMasterDAO = styleMasterDAO;
	}


	public void setStyleColorDAO(IStyleColorDAO styleColorDAO) {
		this.styleColorDAO = styleColorDAO;
	}


	public void setFabricDAO(IFabricDAO fabricDAO) {
		this.fabricDAO = fabricDAO;
	}
	
	@Override
	public ResultContext saveStyle(RequestContext rqs) throws BaseException {
		StyleDTO style = (StyleDTO)rqs.getParameter(DesignTokens.KEY_STYLE_DTO);
		UserDTO user = rqs.getUser();
		style.setOrgId(user.getOrgId());
		
		//1. 处理款式记录
		if(style.getId() >0){
			//***************************
			//更新记录
			//***************************
			logger.debug("Update Style Record");
			
			//取回更新前的记录
			StyleDTO original = styleMasterDAO.retrieveStyle(style.getId());
			//取回寻料主键备用
			Integer taskId = inquiryTaskDAO.retrieveTaskIdByStyleId(style.getId());
			//如果主料辅料元素变化，或者样板尺码改变，就需要把布样任务设置为OPEN状态
			boolean reOpenSampleTask = false;
			if(!original.getSampleSize().equals(style.getSampleSize())){
				logger.debug("Sample size changed, reopen sample task");
				reOpenSampleTask = true;
			}
			
			//如果主料辅料元素变化，或者颜色要求变化，就需要把寻料任务设置为OPEN状态
			boolean reOpenInquiryTask = false;
			
			//如果专机任务的标志是从N -> Y，专机任务状态UPDATE -> OPEN
			//如果专机任务是从Y ->Y 或者 N - N, 不变
			//如果专机任务是从Y -> N， 专机任务状态 -》 null
			if(original.getSpecialReq().equals(style.getSpecialReq())){
				logger.debug("Do nothing on special task");
			}else{
				if(Constants.DB_YES.equals(style.getSpecialReq()))
					style.setSpecialProgressState(Constants.STATUS_OPEN);
			}
			
			//对比产品颜色
			List<ProductColorDTO > oriColors = styleColorDAO.retrieveColors(style.getId());
			List<ProductColorDTO > newColors = new ArrayList<ProductColorDTO>();
			//新增的颜色有：
			for(ProductColorDTO color: style.getColors()){
				if(!oriColors.contains(color)){
					//增加颜色记录
					StyleColorDTO sc = new StyleColorDTO();
					sc.setStyleId(style.getId());
					sc.setColorId(color.getId());
					sc.setOrgId(user.getOrgId());
					sc = styleColorDAO.createStyleColor(sc);
					logger.debug("color updated - " + color.getName());
					//准备为每种原料增加新的寻料记录
					newColors.add(color);
				}
			}

			//需要删除的颜色有
			for(ProductColorDTO oriColor:oriColors){
				if(! style.getColors().contains(oriColor)){
					styleColorDAO.deleteColor(style.getId(), oriColor.getId());
					// 删除该颜色的所有寻料记录
					inquiryItemDAO.deleteInquiryItemBelongTo(style.getId(), oriColor.getId());
				}
			}
			
			//原料

			List<FabricDTO> originalPrimaryFab = original.getPrimaryFab();
			for(FabricDTO fab: style.getPrimaryFab()){
				if(originalPrimaryFab.contains(fab)){
					logger.debug("Update Primary Fabric");
					//已有，更新
					fabricDAO.updateSimpleFabric(fab);
					
					
					FabricDTO originalFabric = originalPrimaryFab.get(originalPrimaryFab.indexOf(fab));
					
					if(! originalFabric.getColoredInd().equals(fab.getColoredInd())){
						//开始选择不区分颜色，现在要区分颜色,
						//或者开始区分，现在不区分
						//删除旧的全部记录，增加新的寻料记录
						inquiryItemDAO.deleteInquiryItemBelongTo(fab.getId());
						createInquiryItem(fab.getId(), taskId, user.getOrgId(), fab.getColoredInd(), style.getColors());
						reOpenInquiryTask = true;
					}else{
						//否则,如果有新增的颜色，而此原料要求区分颜色的话，增加新颜色的寻料记录
						if(!newColors.isEmpty() && Constants.DB_YES.equals(fab.getColoredInd())){
							createInquiryItem(fab.getId(), taskId, user.getOrgId(), fab.getColoredInd(), newColors);
							logger.debug("Create inquiry item for added colors.");
							reOpenInquiryTask = true;
						}
					}

				}else{
					logger.debug("New Primary Fabric");
					//没有，增加
					fab.setType(Constants.PRIMARY_FABRIC);
					fab.setStyleId(style.getId());
					fab.setOrgId(user.getOrgId());
					fab = fabricDAO.createSimpleFabric(fab);
					logger.debug("primary fabric saved: " + fab.getName());
					//增加对应的寻料记录
					createInquiryItem(fab.getId(), taskId, user.getOrgId(), fab.getColoredInd(), style.getColors());
					logger.debug("Inquiry Task Item created.");
					reOpenInquiryTask = true;
					reOpenSampleTask = true;
				}
			}
			for(FabricDTO orgFab : originalPrimaryFab){
				if(!style.getPrimaryFab().contains(orgFab)){
					logger.debug("Delete Primary Fabric");
					//现在没有，删除
					fabricDAO.deleteFabric(orgFab.getId());
					//根据数据库的外键关联，寻料记录自动删除
				}
			}
			//辅料
			List<FabricDTO> originalOtherFab = original.getSecondaryFab();
			for(FabricDTO fab: style.getSecondaryFab()){
				if(originalOtherFab.contains(fab)){
					logger.debug("Update other Fabric");
					//已有，更新
					fabricDAO.updateSimpleFabric(fab);
					
					FabricDTO originalFabric = originalOtherFab.get(originalOtherFab.indexOf(fab));
					
					if(! originalFabric.getColoredInd().equals(fab.getColoredInd())){
						//开始选择不区分颜色，现在要区分颜色,
						//或者开始区分，现在不区分
						//删除旧的全部记录，增加新的寻料记录
						inquiryItemDAO.deleteInquiryItemBelongTo(fab.getId());
						createInquiryItem(fab.getId(), taskId, user.getOrgId(), fab.getColoredInd(), style.getColors());
						reOpenInquiryTask = true;
					}else{
						//否则,如果有新增的颜色，而此原料要求区分颜色的话，增加新颜色的寻料记录
						if(!newColors.isEmpty() && Constants.DB_YES.equals(fab.getColoredInd())){
							createInquiryItem(fab.getId(), taskId, user.getOrgId(), fab.getColoredInd(), newColors);
							logger.debug("Create inquiry item for added colors.");
							reOpenInquiryTask = true;
						}
					}
				}else{
					logger.debug("New other Fabric");
					//没有，增加
					fab.setType(Constants.SECONDARY_FABRIC);
					fab.setStyleId(style.getId());
					fab.setOrgId(user.getOrgId());
					fab = fabricDAO.createSimpleFabric(fab);
					logger.debug("primary other saved: " + fab.getName());
					//增加对应的寻料记录
					createInquiryItem(fab.getId(), taskId, user.getOrgId(), fab.getColoredInd(), style.getColors());
					logger.debug("Inquiry Task Item created.");
					reOpenInquiryTask = true;
					reOpenSampleTask = true;
				}
			}
			for(FabricDTO orgFab : originalOtherFab){
				if(!style.getSecondaryFab().contains(orgFab)){
					logger.debug("Delete other Fabric");
					//现在没有，删除
					fabricDAO.deleteFabric(orgFab.getId());
					//根据数据库的外键关联，寻料记录自动删除
				}
			}
			
			if(reOpenSampleTask){
				style.setSampleState(Constants.STATUS_OPEN);
				style.setDressState(Constants.STATUS_OPEN);
			}
			
			//如果reOpenInquiryTask = true; 将寻料任务更改为OPEN状态
			if(reOpenInquiryTask)
				inquiryTaskDAO.updateInquiryTaskState(taskId);
			
			
			//Save master table
			styleMasterDAO.updateStyle(style);
			
		}else{
			//创建新款号
			logger.debug("create style Record");
			//根据Year+season获取流水号
			int seq = sequenceDAO.getSequence(Sequence.STYLE_SEQ, style.getYear(), null, style.getSeason());
			//生成款号，格式 yy-季节编号+000
			SeasonDTO seasonDTO = seasonDAO.retreiveSeason(style.getSeason());
			DecimalFormat nf = new DecimalFormat("000");
			String code = StringUtils.substring(Integer.toString(style.getYear()), 2)
					+ "-" + seasonDTO.getCode() + nf.format(seq)  ;
			logger.debug("New Style code generated: " + code);
			
			style.setCode(code);
			style.setState(Constants.STYLE_STATE_NEW);
			
			//save master table
			style = styleMasterDAO.createStyle(style);
			
			//创建寻料任务
			InquiryTaskDTO inquiryTask = new InquiryTaskDTO();
			inquiryTask.setOrgId(user.getOrgId());
			inquiryTask.setStyleId(style.getId());
			inquiryTask.setState(Constants.STATUS_OPEN);
			inquiryTask.setCreationDate(new Date());
			inquiryTask = inquiryTaskDAO.createInquiryTaskDTO(inquiryTask);
			logger.info("Inquiry Task created for the newly style.");
			
			//Update style - color mapping
			for(ProductColorDTO color : style.getColors()){
				StyleColorDTO sc = new StyleColorDTO();
				sc.setStyleId(style.getId());
				sc.setColorId(color.getId());
				sc.setOrgId(user.getOrgId());
				sc = styleColorDAO.createStyleColor(sc);
				logger.debug("color saved - " + color.getName());
			}
			//Update Fabric
			for(FabricDTO fabric: style.getPrimaryFab()){
				fabric.setType(Constants.PRIMARY_FABRIC);
				fabric.setStyleId(style.getId());
				fabric.setOrgId(user.getOrgId());
				fabric = fabricDAO.createSimpleFabric(fabric);
				logger.debug("primary fabric saved: " + fabric.getName());
				//创建对应的寻料任务的item
				createInquiryItem(fabric.getId(), inquiryTask.getId(), user.getOrgId(), fabric.getColoredInd(), style.getColors());
				logger.debug("Inquiry Task Item created.");
			}
			for(FabricDTO fabric: style.getSecondaryFab()){
				fabric.setType(Constants.SECONDARY_FABRIC);
				fabric.setStyleId(style.getId());
				fabric.setOrgId(user.getOrgId());
				fabric = fabricDAO.createSimpleFabric(fabric);
				logger.debug("secondary fabric saved: " + fabric.getName());
				//创建对应的寻料任务的item
				createInquiryItem(fabric.getId(), inquiryTask.getId(), user.getOrgId(), fabric.getColoredInd(), style.getColors());
				logger.debug("Inquiry Task Item created.");
			}			
		}//End if(style.getId() >0)
		
		logger.info("[Style Mgmt] 1. Style record saved: " + style.getCode());
		
		//2. 纸板任务
		//2.1是否有软样任务？
		List<TemplateDTO> softTempList = templateDAO.retrieveTemplates(style.getId(), Constants.TEMPLATE_SOFT);
		if(softTempList != null && !softTempList.isEmpty()){
			//是，软样任务的size是不是和sampleSize一样？
			TemplateDTO softTemplate = softTempList.get(0);
			if(softTemplate.getSize().equalsIgnoreCase(style.getSampleSize())){				
				//是，不做任何动作
				logger.info("[Style Mgmt] Soft sample is not change, nothing need to do");
			}else{
				//否，删除软样记录，GOTO 新建软样记录
				logger.info("[Style Mgmt] Soft sample changed, delete the original one, size: " + softTemplate.getSize());
				templateDAO.deleteTemplate(softTemplate.getId());
				logger.info("[Style Mgmt] Create another soft template, size: " + style.getSampleSize());
				createTemplateTask(style, style.getSampleSize(), Constants.TEMPLATE_SOFT);
			}
		}else{
			//否，新建软样记录
			logger.info("[Style Mgmt] Create a soft template");
			createTemplateTask(style, style.getSampleSize(), Constants.TEMPLATE_SOFT);
		}
		
		//2.2 是否有生产尺码的要求
		if(style.getSizes() != null && !style.getSizes().isEmpty()){
			//是否已有硬样任务？			
			List<TemplateDTO> hardTempList = templateDAO.retrieveTemplates(style.getId(), Constants.TEMPLATE_HARD);
			if(hardTempList != null && !hardTempList.isEmpty()){
				//是，已有的硬样任务是否在现在的生产尺码中？
				for(TemplateDTO t : hardTempList){
					if(!style.getSizes().contains(t.getSize())){
						//否，删除该硬样任务
						logger.info("[Style Mgmt] Hard sample changed, delete the original one, size:" + t.getSize());
						templateDAO.deleteTemplate(t.getId());
					}
				}
				//有新的生产尺码吗？
				for(String size:style.getSizes()){
					boolean isNew = true;
					for(TemplateDTO existingTemp:hardTempList){
						if(existingTemp.getSize().equals(size)){
							isNew = false;
							break;
						}
					}
					if(isNew){
						//是，创建新的硬样任务
						createTemplateTask(style, size, Constants.TEMPLATE_HARD);
						logger.info("[Style Mgmt] Hard sample created, size: " + size);
					}
					
				}
			}else{
				//否，为生产尺码创建对应的硬样任务
				for(String size:style.getSizes()){
					createTemplateTask(style, size, Constants.TEMPLATE_HARD);
					logger.info("[Style Mgmt] Hard sample created, size: " + size);
				}
			}
		}
		logger.info("[Style Mgmt] 2. Template tasks saved: " + style.getCode());
		
		ResultContext rlt = new ResultContext();
		rlt.addResult(DesignTokens.KEY_STYLE_DTO, style);
		return rlt;
	}
	
	private void createInquiryItem(Integer fabricId, Integer taskId, Integer orgId, String hasColorReq, List<ProductColorDTO> colors){
		if(Constants.DB_YES.equals(hasColorReq)){
			//区分颜色的寻料任务
			for(ProductColorDTO c : colors){
				InquiryItemDTO item = new InquiryItemDTO();
				item.setFabricId(fabricId);
				item.setOrgId( orgId);
				item.setTaskId(taskId);
				item.getProductColor().setId(c.getId());
				inquiryItemDAO.createInquiryItem(item);
			}
		}else{
			//不区分颜色的寻料任务
			InquiryItemDTO item = new InquiryItemDTO();
			item.setFabricId(fabricId);
			item.setOrgId( orgId);
			item.setTaskId(taskId);
			inquiryItemDAO.createInquiryItem(item);
		}

	}
	private void createTemplateTask(StyleDTO style, String size, String type){
		TemplateDTO temp = new TemplateDTO();
		temp.setOrgId(style.getOrgId());
		temp.setCreationDate(new Date());
		temp.setSize(size);
		temp.setStyleId(style.getId());
		temp.setType(type);
		temp.setState(Constants.STATUS_OPEN);
		templateDAO.createTemplate(temp);
	}
	
	@Override
	public ResultContext retrievePagedStyles(RequestContext rqs) {
		UserDTO user = rqs.getUser();
		String searchCode = (String) rqs.getParameter(DesignTokens.KEY_STYLE_CODE);
		String searchState = (String) rqs.getParameter(DesignTokens.KEY_STYLE_STATE);
		Integer pageNo = (Integer) rqs.getParameter(Tokens.KEY_PAGE_NO);
		Integer pageSize = (Integer) rqs.getParameter(Tokens.KEY_PAGE_SIZE);
		
		List<StyleDTO> lst = styleMasterDAO.retrievePagedStyles(searchCode, searchState, user.getOrgId(), pageNo, pageSize);

		ResultContext rlt = new ResultContext();
		rlt.addResult(DesignTokens.KEY_STYLE_LIST, lst);
		return rlt;
	}

	@Override
	public ResultContext retrieveStyleRecordCount(RequestContext rqs) {
		UserDTO user = rqs.getUser();
		String searchCode = (String) rqs.getParameter(DesignTokens.KEY_STYLE_CODE);
		String searchState = (String) rqs.getParameter(DesignTokens.KEY_STYLE_STATE);
		Integer rs =  styleMasterDAO.retrieveStyleRecordCount(searchCode, searchState, user.getOrgId());
		ResultContext rlt = new ResultContext();
		rlt.addResult(Tokens.KEY_RECORD_COUNT, rs);
		return rlt;
	}

	@Override
	public ResultContext retrieveStyleById(RequestContext rqs) {
		Integer id = (Integer) rqs.getParameter(DesignTokens.KEY_STYLE_ID);
		StyleDTO to = styleMasterDAO.retrieveStyle(id);
		ResultContext rlt = new ResultContext();
		rlt.addResult(DesignTokens.KEY_STYLE_DTO, to);
		return rlt;
	}



	@Override
	public ResultContext retrievePagedTemplates(RequestContext rqs) {
		UserDTO user = rqs.getUser();
		String searchCode = (String) rqs.getParameter(DesignTokens.KEY_STYLE_CODE);
		String searchState = (String) rqs.getParameter(DesignTokens.KEY_TEMP_STATE);
		Integer pageNo = (Integer) rqs.getParameter(Tokens.KEY_PAGE_NO);
		Integer pageSize = (Integer) rqs.getParameter(Tokens.KEY_PAGE_SIZE);
		
		List<TemplateDTO> lst = templateDAO.retrievePagedTempaltes(searchCode, searchState, user.getOrgId(), pageNo, pageSize);

		ResultContext rlt = new ResultContext();
		rlt.addResult(DesignTokens.KEY_TEMP_LIST, lst);
		return rlt;
	}

	@Override
	public ResultContext retrieveTemplatesRecordCount(RequestContext rqs) {
		UserDTO user = rqs.getUser();
		String searchCode = (String) rqs.getParameter(DesignTokens.KEY_STYLE_CODE);
		String searchState = (String) rqs.getParameter(DesignTokens.KEY_TEMP_STATE);
		Integer rs = templateDAO.retrieveTemplatesRecordCount(searchCode, searchState, user.getOrgId());
		ResultContext rlt = new ResultContext();
		rlt.addResult(Tokens.KEY_RECORD_COUNT, rs);
		return rlt;
	}

	@Override
	public ResultContext saveTemplate(RequestContext rqs) throws BaseException {
		UserDTO user = rqs.getUser();
		TemplateDTO template = (TemplateDTO) rqs.getParameter(DesignTokens.KEY_TEMP_DTO);		
		if(template != null){
			logger.debug("Prepare to update template");
			templateDAO.updateTemplate(template);
			List<TemplatePartDTO> details = template.getParts();
			//删除需要删除的
			List<TemplatePartDTO> existing = templatePartDAO.retrieveTemplatePart(template.getId());
			if(existing != null && !existing.isEmpty()){
				for(TemplatePartDTO ot : existing){
					if(details == null || !details.contains(ot)){
						logger.debug("remove exisitng template part");
						templatePartDAO.deleteTemplatePart(ot.getId());
					}
				}
			}
			
			if(details != null){
				for(TemplatePartDTO t : details){
					if(t.getId() > 0){
						logger.debug("Prepare to update template part");
						templatePartDAO.updateTemplatePart(t);
					}else{
						logger.debug("Prepare to insert template part");
						t.setOrgId(user.getOrgId());
						t.setTemplateId(template.getId());
						templatePartDAO.createTemplatePart(t);
					}	
				}				
			}
		}
		
		return new ResultContext();
	}

	@Override
	public ResultContext retrieveTemplateDetails(RequestContext rqs) {
		Integer templateId = (Integer) rqs.getParameter(DesignTokens.KEY_TEMP_ID);
		List<TemplatePartDTO> existing = templatePartDAO.retrieveTemplatePart(templateId);
		ResultContext rlt = new ResultContext();
		rlt.addResult(DesignTokens.KEY_TEMP_DTL_LST, existing);
		return rlt;
	}

	@Override
	public ResultContext retrieveTemplatesByStyleId(RequestContext rqs) {
		Integer styleId = (Integer) rqs.getParameter(DesignTokens.KEY_STYLE_ID);
		Integer excludedId = (Integer) rqs.getParameter(DesignTokens.KEY_TEMP_ID);
		String state = (String) rqs.getParameter(DesignTokens.KEY_TEMP_STATE);
		List<TemplateDTO> lst = templateDAO.retrieveTemplatesByStyle(styleId, state, excludedId);
		ResultContext rlt = new ResultContext();
		rlt.addResult(DesignTokens.KEY_TEMP_LIST, lst);
		return rlt;
	}

	@Override
	public ResultContext retrievePagedSampleTasks(RequestContext rqs) {
		UserDTO user = rqs.getUser();
		String searchCode = (String) rqs.getParameter(DesignTokens.KEY_STYLE_CODE);
		String taskState = (String) rqs.getParameter(DesignTokens.KEY_SAMPLE_STATE);
		Integer pageNo = (Integer) rqs.getParameter(Tokens.KEY_PAGE_NO);
		Integer pageSize = (Integer) rqs.getParameter(Tokens.KEY_PAGE_SIZE);
		
		List<StyleDTO> lst = styleMasterDAO.retrievePagedSampleTasks(searchCode, taskState, user.getOrgId(), pageNo, pageSize);

		ResultContext rlt = new ResultContext();
		rlt.addResult(DesignTokens.KEY_STYLE_LIST, lst);
		return rlt;
	}

	@Override
	public ResultContext retrieveSampleTaskCount(RequestContext rqs) {
		UserDTO user = rqs.getUser();
		String searchCode = (String) rqs.getParameter(DesignTokens.KEY_STYLE_CODE);
		String taskState = (String) rqs.getParameter(DesignTokens.KEY_SAMPLE_STATE);
		Integer rs =styleMasterDAO.retrieveSampleTaskCount(searchCode, taskState, user.getOrgId());
		ResultContext rlt = new ResultContext();
		rlt.addResult(Tokens.KEY_RECORD_COUNT, rs);
		return rlt;
	}

	@Override
	public ResultContext saveSampleTask(RequestContext rqs) throws NoRecordUpdateException {
		UserDTO user = rqs.getUser();
		StyleDTO style = (StyleDTO) rqs.getParameter(DesignTokens.KEY_STYLE_DTO);
		styleMasterDAO.updateSampleTask(style);
		for(FabricDTO pf : style.getPrimaryFab()){
			fabricDAO.updateFabricEstimation(pf);
		}
		for(FabricDTO sf : style.getSecondaryFab()){
			fabricDAO.updateFabricEstimation(sf);
		}
		ResultContext rlt = new ResultContext();
		return rlt;
	}

	@Override
	public ResultContext approveSample(RequestContext rqs) throws NoRecordUpdateException {
		UserDTO user = rqs.getUser();
		StyleDTO style = (StyleDTO) rqs.getParameter(DesignTokens.KEY_STYLE_DTO);
		styleMasterDAO.updateDressTask(style);
		ResultContext rlt = new ResultContext();
		return rlt;
	}




}
